package com.sample.wanandroidx.ui.fragment

import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.sample.wanandroidx.adapter.HomeFragmentAdapter
import com.sample.wanandroidx.basic.BaseFragment
import com.sample.wanandroidx.databinding.FragmentClassificationBinding

class ClassificationFragment : BaseFragment<FragmentClassificationBinding>(FragmentClassificationBinding::inflate) {

    override fun initLazyLoad() {
        val titles = listOf("常用", "网站导航", "体系数据", "项目分类", "公众号")
        val fragments = mutableListOf(WebSiteFragment(),
            NaviFragment(),
            TreeFragment(),
            HomeFragment.newInstance(3),
            WebSiteFragment())
        activity?.let {
            binding.pager.adapter = HomeFragmentAdapter(it, fragments)
            binding.pager.offscreenPageLimit = titles.size - 1
            val tabLayoutMediator = TabLayoutMediator(binding.tab, binding.pager) { tab, position ->
                tab.text = titles[position]
            }
            tabLayoutMediator.attach()
            setViewpager2(binding.pager)
        }
    }

    override fun initialization() {
    }

    fun setViewpager2(viewpager2: ViewPager2?) {
        try {
            val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
            recyclerViewField.isAccessible = true
            val recyclerView = recyclerViewField[viewpager2] as RecyclerView
            val touchSlopField = RecyclerView::class.java.getDeclaredField("mTouchSlop")
            touchSlopField.isAccessible = true
            val touchSlop = touchSlopField[recyclerView] as Int
            touchSlopField[recyclerView] = touchSlop * 2 //6 is empirical value
        } catch (ignore: Exception) {
        }
    }
}








